/*
 * File: HttpUtil.java
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tvseries.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.log4j.Logger;

public class HttpUtil
{
	private static final Logger log = Logger.getLogger(HttpUtil.class);

	private static HttpUtil instance = null;
	
	private DefaultHttpClient client = null;
	private BasicHttpContext httpContext = null;
	
	private HttpUtil()
	{
		try
		{
			HttpParams params = new BasicHttpParams();
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(params, true);
			HttpProtocolParams.setUserAgent(params, PropUtil.get("http.useragent"));

			client = new DefaultHttpClient();
			client.setParams(params);

			httpContext = new BasicHttpContext();
		}
		catch (Exception e)
		{
			log.error("Failed to instantiate HttpUtils.");
		}
	}

	public static HttpUtil getInstance()
	{
		if (instance == null)
		{
			instance = new HttpUtil();
			log.info("Instantiating HttpUtils.");
		}

		return instance;
	}
	
	public String get(String strUrl) throws ClientProtocolException, IOException, URISyntaxException
	{
		URI uri = new URI(strUrl);
		
		HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
		HttpGet httpGet = new HttpGet(uri.getPath());

		HttpResponse response = client.execute(httpHost, httpGet, httpContext);
		int sc = response.getStatusLine().getStatusCode(); 
		String phrase = response.getStatusLine().getReasonPhrase();
		if (sc < 200 || sc >= 400)
			throw new ClientProtocolException("Error [Status Code = " + sc + "]" + ": " + phrase);
		
		InputStream is = response.getEntity().getContent();
		
		ByteArrayOutputStream data = new ByteArrayOutputStream();
		int len = 0;
		byte[] buf = new byte[1024];
		while ((len = is.read(buf)) >= 0)
			data.write(buf, 0, len);
		
		return new String(data.toByteArray());
	}
}
